home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / cc-mode / cc-mode.el.z / cc-mode.el
Encoding:
Text File  |  1998-05-21  |  14.1 KB  |  444 lines

  1. ;;; cc-mode.el --- major mode for editing C, C++, Objective-C, and Java code
  2.  
  3. ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
  4.  
  5. ;; Authors:    1992-1997 Barry A. Warsaw
  6. ;;             1987 Dave Detlefs and Stewart Clamen
  7. ;;             1985 Richard M. Stallman
  8. ;; Maintainer: cc-mode-help@python.org
  9. ;; Created:    a long, long, time ago. adapted from the original c-mode.el
  10. ;; Keywords:   c languages oop
  11.  
  12. (defconst c-version "5.20"
  13.   "CC Mode version number.")
  14.  
  15. ;; NOTE: Read the commentary below for the right way to submit bug reports!
  16. ;; NOTE: See the accompanying texinfo manual for details on using this mode!
  17.  
  18. ;; This file is part of GNU Emacs.
  19.  
  20. ;; GNU Emacs is free software; you can redistribute it and/or modify
  21. ;; it under the terms of the GNU General Public License as published by
  22. ;; the Free Software Foundation; either version 2, or (at your option)
  23. ;; any later version.
  24.  
  25. ;; GNU Emacs is distributed in the hope that it will be useful,
  26. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. ;; GNU General Public License for more details.
  29.  
  30. ;; You should have received a copy of the GNU General Public License
  31. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  32. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  33. ;; Boston, MA 02111-1307, USA.
  34.  
  35. ;;; Commentary:
  36.  
  37. ;; This package provides GNU Emacs major modes for editing C, C++,
  38. ;; Objective-C, Java and IDL code.  As of the latest Emacs and XEmacs
  39. ;; releases, it is the default package for editing these languages.
  40. ;; This package is called "CC Mode", and should be spelled exactly
  41. ;; this way.  It supports K&R and ANSI C, ANSI C++, Objective-C, Java,
  42. ;; and CORBA's IDL with a consistent indentation model across all
  43. ;; modes.  This indentation model is intuitive and very flexible, so
  44. ;; that almost any desired style of indentation can be supported.
  45. ;; Installation, usage, and programming details are contained in an
  46. ;; accompanying texinfo manual.
  47.  
  48. ;; CC Mode's immediate ancestors were, c++-mode.el, cplus-md.el, and
  49. ;; cplus-md1.el..
  50.  
  51. ;; NOTE: This mode does not perform font-locking (a.k.a syntactic
  52. ;; coloring, keyword highlighting, etc.) for any of the supported
  53. ;; modes.  Typically this is done by a package called font-lock.el
  54. ;; which I do *not* maintain.  You should contact the Emacs
  55. ;; maintainers for questions about coloring or highlighting in any
  56. ;; language mode.
  57.  
  58. ;; To submit bug reports, type "C-c C-b".  These will be sent to
  59. ;; bug-gnu-emacs@prep.ai.mit.edu as well as cc-mode-help@python.org,
  60. ;; and I'll read about them there (the former is mirrored as the
  61. ;; Usenet newsgroup gnu.emacs.bug).  Questions can sent to
  62. ;; help-gnu-emacs@prep.ai.mit.edu (mirrored as gnu.emacs.help) and/or
  63. ;; cc-mode-help@python.org.  Please do not send bugs or questions to
  64. ;; my personal account.
  65.  
  66. ;; Many, many thanks go out to all the folks on the beta test list.
  67. ;; Without their patience, testing, insight, code contributions, and
  68. ;; encouragement CC Mode would be a far inferior package.
  69.  
  70. ;; You can get the latest version of CC Mode, including PostScript
  71. ;; documentation and separate individual files from:
  72. ;;
  73. ;;     http://www.python.org/ftp/emacs/
  74.  
  75. ;; Or if you don't have access to the World Wide Web, through
  76. ;; anonymous ftp from:
  77. ;;
  78. ;;    ftp://ftp.python.org/pub/emacs
  79.  
  80. ;;; Code:
  81.  
  82. (eval-when-compile
  83.   (require 'cc-defs))
  84.  
  85. ;; sigh.  give in to the pressure, but make really sure all the
  86. ;; definitions we need are here
  87. (if (or (not (fboundp 'functionp))
  88.     (not (fboundp 'char-before))
  89.     (not (c-safe (char-after) t))
  90.     (not (fboundp 'when))
  91.     (not (fboundp 'unless)))
  92.     (require 'cc-mode-19))
  93.  
  94. (eval-when-compile
  95.   (require 'cc-menus))
  96.  
  97. (defvar c-buffer-is-cc-mode nil
  98.   "Non-nil for all buffers with a `major-mode' derived from CC Mode.
  99. Otherwise, this variable is nil.  I.e. this variable is non-nil for
  100. `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode', and any
  101. other non-CC Mode mode that calls `c-initialize-cc-mode'
  102. \(e.g. `awk-mode').")
  103. (make-variable-buffer-local 'c-buffer-is-cc-mode)
  104. (put 'c-buffer-is-cc-mode 'permanent-local t)
  105.  
  106. (defvar c-initialize-on-load t
  107.   "When non-nil, CC Mode initializes when the cc-mode.el file is loaded.")
  108.   
  109.  
  110.  
  111. ;; Other modes and packages which depend on CC Mode should do the
  112. ;; following to make sure everything is loaded and available for their
  113. ;; use:
  114. ;;
  115. ;; (require 'cc-mode)
  116. ;; (c-initialize-cc-mode)
  117.  
  118. ;;;###autoload
  119. (defun c-initialize-cc-mode (&optional skip-styles)
  120.   (setq c-buffer-is-cc-mode t)
  121.   ;; make sure all necessary components of CC Mode are loaded in.
  122.   (let ((initprop 'cc-mode-is-initialized))
  123.     (require 'cc-vars)
  124.     (require 'cc-engine)
  125.     (require 'cc-langs)
  126.     (require 'cc-menus)
  127.     (require 'cc-align)
  128.     (require 'cc-styles)
  129.     (require 'cc-cmds)
  130.     ;; run the initialization hook, but only once
  131.     (or (get 'c-initialize-cc-mode initprop)
  132.     (progn
  133.       (or skip-styles
  134.           (c-initialize-builtin-style))
  135.       (run-hooks 'c-initialization-hook)
  136.       (put 'c-initialize-cc-mode initprop t)))
  137.     ))
  138.  
  139.  
  140. ;;;###autoload
  141. (defun c-mode ()
  142.   "Major mode for editing K&R and ANSI C code.
  143. To submit a problem report, enter `\\[c-submit-bug-report]' from a
  144. c-mode buffer.  This automatically sets up a mail buffer with version
  145. information already added.  You just need to add a description of the
  146. problem, including a reproducible test case and send the message.
  147.  
  148. To see what version of CC Mode you are running, enter `\\[c-version]'.
  149.  
  150. The hook variable `c-mode-hook' is run with no args, if that value is
  151. bound and has a non-nil value.  Also the hook `c-mode-common-hook' is
  152. run first.
  153.  
  154. Key bindings:
  155. \\{c-mode-map}"
  156.   (interactive)
  157.   (c-initialize-cc-mode)
  158.   (kill-all-local-variables)
  159.   (set-syntax-table c-mode-syntax-table)
  160.   (setq major-mode 'c-mode
  161.     mode-name "C"
  162.     local-abbrev-table c-mode-abbrev-table)
  163.   (use-local-map c-mode-map)
  164.   (c-common-init)
  165.   (setq comment-start "/* "
  166.     comment-end   " */"
  167.     c-conditional-key c-C-conditional-key
  168.     c-class-key c-C-class-key
  169.     c-baseclass-key nil
  170.     c-comment-start-regexp c-C++-comment-start-regexp
  171.     imenu-generic-expression cc-imenu-c-generic-expression
  172.     imenu-case-fold-search nil
  173.     )
  174.   (run-hooks 'c-mode-common-hook)
  175.   (run-hooks 'c-mode-hook)
  176.   (c-update-modeline))
  177.  
  178.  
  179. ;;;###autoload
  180. (defun c++-mode ()
  181.   "Major mode for editing C++ code.
  182. To submit a problem report, enter `\\[c-submit-bug-report]' from a
  183. c++-mode buffer.  This automatically sets up a mail buffer with
  184. version information already added.  You just need to add a description
  185. of the problem, including a reproducible test case, and send the
  186. message.
  187.  
  188. To see what version of CC Mode you are running, enter `\\[c-version]'.
  189.  
  190. The hook variable `c++-mode-hook' is run with no args, if that
  191. variable is bound and has a non-nil value.  Also the hook
  192. `c-mode-common-hook' is run first.
  193.  
  194. Key bindings:
  195. \\{c++-mode-map}"
  196.   (interactive)
  197.   (c-initialize-cc-mode)
  198.   (kill-all-local-variables)
  199.   (set-syntax-table c++-mode-syntax-table)
  200.   (setq major-mode 'c++-mode
  201.     mode-name "C++"
  202.     local-abbrev-table c++-mode-abbrev-table)
  203.   (use-local-map c++-mode-map)
  204.   (c-common-init)
  205.   (setq comment-start "// "
  206.     comment-end ""
  207.     c-conditional-key c-C++-conditional-key
  208.     c-comment-start-regexp c-C++-comment-start-regexp
  209.     c-class-key c-C++-class-key
  210.     c-extra-toplevel-key c-C++-extra-toplevel-key
  211.     c-access-key c-C++-access-key
  212.     c-recognize-knr-p nil
  213.     imenu-generic-expression cc-imenu-c++-generic-expression
  214.     imenu-case-fold-search nil
  215.     )
  216.   (run-hooks 'c-mode-common-hook)
  217.   (run-hooks 'c++-mode-hook)
  218.   (c-update-modeline))
  219.  
  220.  
  221. ;;;###autoload
  222. (defun objc-mode ()
  223.   "Major mode for editing Objective C code.
  224. To submit a problem report, enter `\\[c-submit-bug-report]' from an
  225. objc-mode buffer.  This automatically sets up a mail buffer with
  226. version information already added.  You just need to add a description
  227. of the problem, including a reproducible test case, and send the
  228. message.
  229.  
  230. To see what version of CC Mode you are running, enter `\\[c-version]'.
  231.  
  232. The hook variable `objc-mode-hook' is run with no args, if that value
  233. is bound and has a non-nil value.  Also the hook `c-mode-common-hook'
  234. is run first.
  235.  
  236. Key bindings:
  237. \\{objc-mode-map}"
  238.   (interactive)
  239.   (c-initialize-cc-mode)
  240.   (kill-all-local-variables)
  241.   (set-syntax-table objc-mode-syntax-table)
  242.   (setq major-mode 'objc-mode
  243.     mode-name "ObjC"
  244.     local-abbrev-table objc-mode-abbrev-table)
  245.   (use-local-map objc-mode-map)
  246.   (c-common-init)
  247.   (setq comment-start "// "
  248.     comment-end   ""
  249.     c-conditional-key c-C-conditional-key
  250.     c-comment-start-regexp c-C++-comment-start-regexp
  251.      c-class-key c-ObjC-class-key
  252.     c-baseclass-key nil
  253.     c-access-key c-ObjC-access-key
  254.     c-method-key c-ObjC-method-key
  255.     imenu-create-index-function 'cc-imenu-objc-function
  256.     imenu-case-fold-search nil
  257.     )
  258.   (run-hooks 'c-mode-common-hook)
  259.   (run-hooks 'objc-mode-hook)
  260.   (c-update-modeline))
  261.  
  262.  
  263. ;;;###autoload
  264. (defun java-mode ()
  265.   "Major mode for editing Java code.
  266. To submit a problem report, enter `\\[c-submit-bug-report]' from a
  267. java-mode buffer.  This automatically sets up a mail buffer with
  268. version information already added.  You just need to add a description
  269. of the problem, including a reproducible test case and send the
  270. message.
  271.  
  272. To see what version of CC Mode you are running, enter `\\[c-version]'.
  273.  
  274. The hook variable `java-mode-hook' is run with no args, if that value
  275. is bound and has a non-nil value.  Also the common hook
  276. `c-mode-common-hook' is run first.  Note that this mode automatically
  277. sets the \"java\" style before calling any hooks so be careful if you
  278. set styles in `c-mode-common-hook'.
  279.  
  280. Key bindings:
  281. \\{java-mode-map}"
  282.   (interactive)
  283.   (c-initialize-cc-mode)
  284.   (kill-all-local-variables)
  285.   (set-syntax-table java-mode-syntax-table)
  286.   (setq major-mode 'java-mode
  287.      mode-name "Java"
  288.      local-abbrev-table java-mode-abbrev-table)
  289.   (use-local-map java-mode-map)
  290.   (c-common-init)
  291.   (setq comment-start "// "
  292.      comment-end   ""
  293.      c-conditional-key c-Java-conditional-key
  294.      c-comment-start-regexp c-Java-comment-start-regexp
  295.       c-class-key c-Java-class-key
  296.     c-method-key nil
  297.      c-baseclass-key nil
  298.     c-recognize-knr-p nil
  299.      c-access-key c-Java-access-key
  300.     ;defun-prompt-regexp c-Java-defun-prompt-regexp
  301.     imenu-generic-expression cc-imenu-java-generic-expression
  302.     imenu-case-fold-search nil
  303.     )
  304.   (c-set-style "java")
  305.   (run-hooks 'c-mode-common-hook)
  306.   (run-hooks 'java-mode-hook)
  307.   (c-update-modeline))
  308.  
  309.  
  310. ;;;###autoload
  311. (defun idl-mode ()
  312.   "Major mode for editing CORBA's IDL code.
  313. To submit a problem report, enter `\\[c-submit-bug-report]' from a
  314. idl-mode buffer.  This automatically sets up a mail buffer with
  315. version information already added.  You just need to add a description
  316. of the problem, including a reproducible test case, and send the
  317. message.
  318.  
  319. To see what version of CC Mode you are running, enter `\\[c-version]'.
  320.  
  321. The hook variable `idl-mode-hook' is run with no args, if that
  322. variable is bound and has a non-nil value.  Also the hook
  323. `c-mode-common-hook' is run first.
  324.  
  325. Key bindings:
  326. \\{idl-mode-map}"
  327.   (interactive)
  328.   (c-initialize-cc-mode)
  329.   (kill-all-local-variables)
  330.   (set-syntax-table idl-mode-syntax-table)
  331.   (setq major-mode 'idl-mode
  332.     mode-name "IDL"
  333.     local-abbrev-table idl-mode-abbrev-table)
  334.   (use-local-map idl-mode-map)
  335.   (c-common-init)
  336.   (setq comment-start "// "
  337.     comment-end ""
  338.     c-conditional-key c-C++-conditional-key
  339.     c-comment-start-regexp c-C++-comment-start-regexp
  340.     c-class-key c-C++-class-key
  341.     c-access-key c-C++-access-key
  342.     c-recognize-knr-p nil
  343. ;;    imenu-generic-expression cc-imenu-c++-generic-expression
  344. ;;    imenu-case-fold-search nil
  345.     )
  346.   (run-hooks 'c-mode-common-hook)
  347.   (run-hooks 'idl-mode-hook)
  348.   (c-update-modeline))
  349.  
  350.  
  351. ;; bug reporting
  352.  
  353. (defconst c-mode-help-address
  354.   "bug-gnu-emacs@prep.ai.mit.edu, cc-mode-help@python.org"
  355.   "Address for CC Mode bug reports.")
  356.  
  357. (defun c-version ()
  358.   "Echo the current version of CC Mode in the minibuffer."
  359.   (interactive)
  360.   (message "Using CC Mode version %s" c-version)
  361.   (c-keep-region-active))
  362.  
  363. ;; Get reporter-submit-bug-report when byte-compiling
  364. ;; (eval-when-compile
  365. ;;   (require 'reporter))
  366.  
  367. (defun c-submit-bug-report ()
  368.   "Submit via mail a bug report on CC Mode."
  369.   (interactive)
  370.   (require 'reporter)
  371.   (require 'cc-vars)
  372.   ;; load in reporter
  373.   (let ((reporter-prompt-for-summary-p t)
  374.     (reporter-dont-compact-list '(c-offsets-alist))
  375.     (style c-indentation-style)
  376.     (hook c-special-indent-hook)
  377.     (c-features c-emacs-features))
  378.     (and
  379.      (if (y-or-n-p "Do you want to submit a report on CC Mode? ")
  380.      t (message "") nil)
  381.      (require 'reporter)
  382.      (reporter-submit-bug-report
  383.       c-mode-help-address
  384.       (concat "CC Mode " c-version " ("
  385.           (cond ((eq major-mode 'c++-mode)  "C++")
  386.             ((eq major-mode 'c-mode)    "C")
  387.             ((eq major-mode 'objc-mode) "ObjC")
  388.             ((eq major-mode 'java-mode) "Java")
  389.             )
  390.           ")")
  391.       (let ((vars (list
  392.            ;; report only the vars that affect indentation
  393.            'c-basic-offset
  394.            'c-offsets-alist
  395.            'c-cleanup-list
  396.            'c-comment-only-line-offset
  397.            'c-backslash-column
  398.            'c-delete-function
  399.            'c-electric-pound-behavior
  400.            'c-hanging-braces-alist
  401.            'c-hanging-colons-alist
  402.            'c-hanging-comment-starter-p
  403.            'c-hanging-comment-ender-p
  404.            'c-indent-comments-syntactically-p
  405.            'c-tab-always-indent
  406.            'c-comment-continuation-stars
  407.            'c-label-minimum-indentation
  408.            'defun-prompt-regexp
  409.            'tab-width
  410.            )))
  411.     (if (not (boundp 'defun-prompt-regexp))
  412.         (delq 'defun-prompt-regexp vars)
  413.       vars))
  414.       (function
  415.        (lambda ()
  416.      (insert
  417.       "Buffer Style: " style "\n\n"
  418.       (if hook
  419.           (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
  420.               "c-special-indent-hook is set to '"
  421.               (format "%s" hook)
  422.               ".\nPerhaps this is your problem?\n"
  423.               "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
  424.         "\n")
  425.       (format "c-emacs-features: %s\n" c-features)
  426.       )))
  427.       nil
  428.       "Dear Barry,"
  429.       ))))
  430.  
  431.  
  432. ;; Initialize everything.  This is backwards compatible with older
  433. ;; .emacs files that just did a (require 'cc-mode) and expected
  434. ;; everything to work (e.g. for CC Mode 4).  Maybe this should just
  435. ;; happen by default, but previous versions of CC Mode 5 did not
  436. ;; initialize by default.  I'm really not sure what is the right thing
  437. ;; to do.
  438. (when (and (null (fboundp 'load-gc)) c-initialize-on-load)
  439.   (c-initialize-cc-mode))
  440.  
  441.  
  442. (provide 'cc-mode)
  443. ;;; cc-mode.el ends here
  444.